<?php

namespace ContentOutputBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class DefaultV1Controller
 * @package ContentOutputBundle\Controller
 * @Route("v1.0/standard")
 */
class DefaultV1Controller extends Controller
{
    /**
     * @Route("/book_info")
     * 书籍基本信息
     */
    public function bookInfo()
    {
        $response = new Response();
        $request = Request::createFromGlobals();
        $book_id = $request->query->get("book_id");
        $result = [];
        if (intval(abs($book_id))) {
            $book_info = $this->get("content_output.cache_data_v1")
                ->getbookCacheData($book_id);
            if ($book_info) {
                $book_state=($book_info["status"]==2)?0:1;
                $keywords=explode(";",$book_info["keyword"]);
                $book_tags=implode(",", $keywords);
                $need_pay=($book_info["isVip"]==1)?1:0;
                $new_book_info=[
                    "book_id"=>(string)$book_info["bookId"],
                    "book_name"=>(string)$book_info["shortName"],
                    "introduction"=>(string)$book_info["intro"],
                    "book_state"=>(int)$book_state,
                    "book_tags"=>(string)$book_tags,
                    "author_name"=>(string)$book_info["author_name"],
                    "cover_url"=>(string)$book_info["cover"],
                    "book_need_pay"=>(int)$need_pay,
                    "published_time"=>(int)$book_info["createTime"],
                    "book_last_update_time"=>(int)$book_info["updateTime"],
                    "book_category_id"=>(string)$book_info["cateId"],
                    "charge_mode"=>$book_info["chargeMode"],
                    "price"=>(int)$book_info["price"],
                    "sale_price"=>(int)$book_info["salePrice"],
                    "obid"=>$book_info["obid"],
                    "copyright_id"=>(int)$book_info["copyrightId"],
                    "cp_show"=>(int)$book_info["cpShow"],
                    "is_publish"=>(int)$book_info["isPublish"],
                    "is_vip"=>(int)$book_info["isVip"],
                    "searchable"=>(int)$book_info["searchable"],
                    "show_status"=>(int)$book_info["showStatus"],
                    "create_time"=>(int)$book_info["createTime"],
                    "update_time"=>(int)$book_info["updateTime"],
                    "vip_chapter_update_time"=>(int)$book_info["vipChapterUpdateTime"],
                    "online_time"=>(int)$book_info["onlineTime"]
                ];
                $result["data"] = $new_book_info;
                $result["code"] = 200;
                $result["msg"] = "success";

            } else {
                $result["data"] = [];
                $result["code"] = 404;
                $result["msg"] = "book no exist";
            }
        } else {
            $result["data"] = [];
            $result["code"] = 400;
            $result["msg"] = " book_id error";
        }
        $response->headers->set('Content-Type', "application/json;charset=UTF-8");
        $response->setStatusCode($result["code"]);
        $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
        return $response;
    }


    /**
     * @Route("/book_chapter_list")
     * 书籍章节列表
     */
    public function bookChapterList()
    {

        $request = Request::createFromGlobals();
        $book_id = $request->query->get("book_id");
        $book_id = intval(abs($book_id));
        $result = [
            "code" => 404,
            "data" => [],
            "msg" => "no data"
        ];

        if ($book_id) {
            $book_chapter_list = $this->get("content_output.cache_data_v1")
                ->getBookChapterListCacheData($book_id);
            if ($book_chapter_list) {
                $new_book_chapter_list=[];
                $chapter_order=1;
                foreach($book_chapter_list as $k=>$v)
                {
                    $temp_chapters=[];
                    foreach($v["chapters"] as $index=>$chapter){
                        $need_pay=$chapter["isVip"]!=1?0:1;
                        $temp_chapters[]=[
                            "chapter_id"=>(string)$chapter["chapterId"],
                            "chapter_name"=>(string)$chapter["chapterName"],
                            "chapter_order_number"=>(int)$chapter_order,
                            "chapter_published_time"=>(int)$chapter["pubTime"],
                            "chapter_last_update_time"=>(int)$chapter["updateTime"],
                            "chapter_need_pay"=>(int)$need_pay,
                            "chapter_word_count"=>(int)$chapter['wordCount'],
                            "chapter_is_vip"=>(int)$chapter['isVip'],
                            "chapter_price"=>(int)$chapter['price'],
                            "content_url"=>$chapter['txtUrl'],
                            "chapter_ocid"=>(int)$chapter['ocid'],
                            "illegal_word_count"=>(int)$chapter['illegalWordCount'],
                            "illegal_word_info"=>$chapter['illegalWordInfo'],
                            "chapter_amount"=>(int)$chapter['amount'],
                        ];
                        $chapter_order++;
                    }
                    $new_book_chapter_list[]=[
                        "volume_id"=>(string)$v["volume_id"],
                        "volume_name"=>(string)$v["volume_name"],
                        "volume_order_number"=>(int)$v["volume_order_number"],
                        "chapters"=>$temp_chapters
                    ];
                }
                $result["code"] = 200;
                $result["msg"] = "success";
                $result["data"] = $new_book_chapter_list;
            }
        } else {
            $result["data"] = [];
            $result["code"] = 400;
            $result["msg"] = " book_id error";
        }
        $response = new Response();
        $response->headers->set('Content-Type', "application/json;charset=UTF-8");
        $response->setStatusCode($result["code"]);
        $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
        return $response;
    }

    /**
     * @Route("/book_chapter_content")
     * 书籍章节内容
     */
    public function bookChapterContent()
    {
        $response = new Response();
        $request = Request::createFromGlobals();
        $book_id = $request->query->get("book_id");
        $chapter_id = $request->query->get("chapter_id");

        if (intval(abs($book_id)) && intval(abs($chapter_id))) {

            $book_chapter_content= $this->get("content_output.cache_data_v1")
                ->getBookChpaterContentCacheData($book_id, $chapter_id);

            if ($book_chapter_content["code"]==200) {
                $content=[
                    "book_id"=>(string)$book_chapter_content["data"]["bookId"],
                    "chapter_id"=>(string)$book_chapter_content["data"]["chapterId"],
                    "chapter_content"=>(string)$book_chapter_content["data"]["content"]
                ];
                $result["data"] = $content;
                $result["code"] = 200;
                $result["msg"] = "success";
            } else {
                $result["data"] = [];
                $result["code"] = 404;
                $result["msg"] = $book_chapter_content["msg"];
            }
        }else {
            $result["data"] = [];
            $result["code"] = 400;
            $result["msg"] = " book_id or chapter_id error";
        }
        $response->headers->set('Content-Type', "application/json;charset=UTF-8");
        $response->setStatusCode($result["code"]);
        $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
        return $response;

    }
}
